﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Diagnostics;

namespace proj2
{
    public partial class MainForm : Form
    {
        private int dirNumber = 0;
        private int currDirNumber = 0;
        private IList<int> lastViewed = new List<int>();
        private string[] files;
        private IList<string> currPath = new List<string>();
        private bool isExpand = false;

        public MainForm()
        {
            InitializeComponent();

        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            var dialog = new folderDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                addNewDir(dialog.directories, dialog.files, dialog.dirCount, dialog.path, dialog.checkBoxState, false);
            }

        }

        private void pictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var dialog = new fullScreen();
            dialog.ShowDialog(pictureBox.Image);
        }

        #region directories
        private void addNewDir(string[] _directories, string[] _files, int _dirCount, string _path, bool _checkBoxState, bool subDir)
        {
            ImageList resList = new ImageList();
            resList.Images.Add(new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
                        GetManifestResourceStream("proj2.Resources.dir.png")));

            resList.Images.Add(new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
                        GetManifestResourceStream("proj2.Resources.imageDir.png")));

            files = _files;
            currPath.Add(_path);
            treeView.ImageList = resList;
            listView.SmallImageList = resList;
            lastViewed.Add(0);

            ListViewItem newItem = new ListViewItem(new[] { _directories[_directories.Length - 1], _dirCount.ToString() }, 1);
            
            listView.Items.Add(newItem);
            listView.Items[0].Position = new Point(listView.Items[0].Position.X + 100, listView.Items[0].Position.Y) ;

            if (files.Length != 0)
            {
                currDirNumber = dirNumber;
                pictureBox.ImageLocation = string.Format("{0}", _path + files[lastViewed[currDirNumber]]);
                Debug.WriteLine(currDirNumber);
                updateButtons();
                number.Visible = true;
                nextButton.Visible = true;
                prevButton.Visible = true;
                pictureBox.Visible = true;
            }
            else
            {
                currDirNumber = dirNumber;
                pictureBox.Visible = false;
                number.Visible = false;
                nextButton.Visible = false;
                prevButton.Visible = false;
            }

            if (!subDir) // idziemy po wszystkich?
            {
                GenerateTreeView(_path, _directories, treeView.Nodes, 0, _checkBoxState);
                currDirNumber = dirNumber;
                if (!isExpand)
                    treeView.ExpandAll();
            }

            dirNumber++;

        }

        public void createDir(string path, out string[] _directories, out string[] _files, out int _dirCount)
        {
            int i = 0;
            string[] images = Directory.GetFiles(@path, "*.*");
            _directories = path.Split('\\');
            _files = new string[images.Length];
            foreach (string s in images)
            {
                _files[i++] = s.Substring(s.LastIndexOf('\\'));
            }
            _dirCount = _files.Length;
        }

        private void subdirectories(string path, TreeNodeCollection nodes)
        {
            string[] dirs = Directory.GetDirectories(path);
            int subDirCount = 0;
            string[] subDirs;
            string[] subFiles;
            int counter = 0;

            foreach (string p in dirs)
            {
                createDir(p, out subDirs, out subFiles, out subDirCount);
                addNewDir(subDirs, subFiles, subDirCount, string.Format("{0}", path + "\\" + subDirs[subDirs.Length - 1]), false, true);


                bool isConstains = false;
                foreach (TreeNode t in nodes)
                {

                    if (t.Text == p.Substring(path.Length + 1))
                    {
                        isConstains = true;
                    }
                }

                if (!isConstains)
                {
                    nodes.Add(p.Substring(p.LastIndexOf('\\') + 1), p.Substring(p.LastIndexOf('\\') + 1), 1, 1);
                    nodes[nodes.Count - 1].Tag = dirNumber;

                    subdirectories(p, nodes[counter++].Nodes);
                }
                
                
            }
        }

        private void GenerateTreeView(string rootPath, string[] dirNames, TreeNodeCollection nodes, int i, bool subDirChecked)
        {
            if (i == dirNames.Length)
            {
                if (subDirChecked)
                {
                    if (isExpand && treeView.Nodes.Count > 1)
                    {
                        treeView.Nodes[treeView.Nodes.Count - 1].ExpandAll();
                    }
                    subdirectories(rootPath, nodes);
                }
                return;
            }
            int idImg = i == dirNames.Length - 1 ? 1 : 0;
            if (nodes.Count == 0)
            {
                var node = new TreeNode(dirNames[i], idImg, idImg);
                if (idImg == 1)
                    node.Tag = dirNumber;
                nodes.Add(node);

                GenerateTreeView(rootPath, dirNames, nodes[0].Nodes, i + 1, subDirChecked);
            }
            else
            {
                int ndId = 0;
                foreach (TreeNode t in nodes)
                {
                    if (dirNames[i] == t.Text)
                    {
                        isExpand = true;
                        GenerateTreeView(rootPath, dirNames, nodes[ndId].Nodes, i + 1, subDirChecked);
                        break;
                    }
                    ndId++;
                }
                if (ndId == nodes.Count)
                {
                    var node = new TreeNode(dirNames[i], idImg, idImg);
                    if (idImg == 1)
                        node.Tag = dirNumber;
                    nodes.Add(node);
                    GenerateTreeView(rootPath, dirNames, nodes[ndId].Nodes, i + 1, subDirChecked);
                }
            }
        }
        #endregion
        #region check and updates
        private void treeRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            listView.Visible = !treeRadioButton.Checked;
        }

        private void historyRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            treeView.Visible = !historyRadioButton.Checked;
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            pictureBox.ImageLocation = string.Format("{0}", currPath[currDirNumber] + files[++lastViewed[currDirNumber]]);
            updateButtons();
        }

        private void prevButton_Click(object sender, EventArgs e)
        {
            pictureBox.ImageLocation = string.Format("{0}", currPath[currDirNumber] + files[--lastViewed[currDirNumber]]);
            updateButtons();
        }

        private void updateButtons()
        {
            prevButton.Enabled = !(lastViewed[currDirNumber] == 0);
            nextButton.Enabled = !(lastViewed[currDirNumber] == files.Length - 1);
            number.Text = string.Format("{0}", lastViewed[currDirNumber] + 1);
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedIndexCollection indexes = this.listView.SelectedIndices;
            foreach (int i in indexes)
            {
                currDirNumber = i;
            }
            correctDirectories(currDirNumber);

        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                currDirNumber = (int)e.Node.Tag;
                correctDirectories(currDirNumber);

            }
        }
        void correctFiles()
        {
            int i = 0;
            string[] images = Directory.GetFiles(@currPath[currDirNumber], "*.*");
            files = new string[images.Length];
            foreach (string s in images)
            {
                files[i++] = s.Substring(s.LastIndexOf('\\'));
            }
        }

        private void correctDirectories(int id)
        {
            correctFiles();
            if (files.Length == 0)
                pictureBox.Visible = prevButton.Visible = number.Visible = nextButton.Visible = false;
            else
            {
                pictureBox.ImageLocation = string.Format("{0}", currPath[currDirNumber] + files[lastViewed[currDirNumber]]);
                updateButtons();
                pictureBox.Visible = prevButton.Visible = number.Visible = nextButton.Visible = true;
            }
        }
        #endregion







    }
}
